package com.itbaizhan.shopping_seckill_customer_api.controller;

/**
 * @author spf
 * @date 2024/1/21
 * @time 17:48
 */

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itbaizhan.shopping_common.pojo.Orders;
import com.itbaizhan.shopping_common.pojo.SeckillGoods;
import com.itbaizhan.shopping_common.result.BaseResult;
import com.itbaizhan.shopping_common.service.OrdersService;
import com.itbaizhan.shopping_common.service.SecKillService;
import com.itbaizhan.shopping_common.utils.JWTUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

/**
 * 秒杀商品控制器API
 */
@RestController
@RequestMapping("/user/seckillGoods")
public class SecKillController {

    @Reference
    private SecKillService secKillService;
    @Reference
    private OrdersService ordersService;

    @GetMapping("/findPage")
    public BaseResult<Page<SeckillGoods>> findPage(int page, int size){
        Page<SeckillGoods> goodsPage = secKillService.findPageByRedis(page, size);
        return BaseResult.ok(goodsPage);
    }

    @GetMapping("/findById")
    public BaseResult<SeckillGoods> findById(Long id){
        SeckillGoods seckillGoods = secKillService.findSecKillGoodsByRedis(id);
        return BaseResult.ok(seckillGoods);
    }

    @PostMapping("/add")
    public BaseResult<Orders> add(@RequestHeader String token, @RequestBody Orders orders){
        Long userId = JWTUtil.getId(token);
        orders.setUserId(userId);
        Orders order = secKillService.createOrder(orders);
        return BaseResult.ok(order);
    }

    @GetMapping("/findOrder")
    public BaseResult<Orders> findOrder(String id){
        Orders order = secKillService.findOrder(id);
        return BaseResult.ok(order);
    }

    @GetMapping("/pay")
    public BaseResult pay(String id){
        Orders orders = secKillService.pay(id);
        ordersService.add(orders);
        return BaseResult.ok();
    }

}
